package ph.com.gs3.formalistics.service.services;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicator;
import ph.com.gs3.formalistics.model.communicators.MessagingCommunicatorModule;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicatorModule.CommunicatorException;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.model.valueobjects.business.Message;
import ph.com.gs3.formalistics.model.valueobjects.business.MessageThread;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.service.managers.MessagingManager;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;

public class FetchMessagesService extends IntentService {

	public static final String TAG = FetchMessagesService.class.getSimpleName();
	public static final String NAME = FetchMessagesService.class.getName();

	public static final String ACTION_ON_THREADS_FETCHED = NAME + "_on_threads_fetched";
	public static final String ACTION_ON_MESSAGES_FOR_THREAD_FETCHED = NAME
	        + "_on_messages_for_thread_fetched";
	public static final String ACTION_ON_ALL_MESSAGES_FETCHED = NAME + "_on_all_messages_fetched";

	public static final String EXTRA_CURRENT_USER = "current_user";
	public static final String EXTRA_FETCHED_THREADS = "fetched_threads";
	public static final String EXTRA_MESSAGES_THREAD_ID = "messages_thread_id";
	public static final String EXTRA_FETCHED_MESSAGES = "fetched_messages";

	private static boolean isRunning;

	private User currentUser;
	private MessagingManager messagingManager;

	public FetchMessagesService() {
		super(TAG);
	}

	@Override
	protected void onHandleIntent(Intent intent) {

		isRunning = true;

		initialize(intent);

		// fetch threads
		List<MessageThread> threads = messagingManager.getCurrentUserThreadList();
		broadcastThreadsFetched(threads);

		// fetch message of each thread
		
		for (MessageThread thread : threads) {
			
		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isRunning = false;
	}

	private void initialize(Intent intent) {

		Bundle extras = intent.getExtras();
		currentUser = (User) extras.getSerializable(EXTRA_CURRENT_USER);

		HttpCommunicator communicator = new HttpCommunicator();
		MessagingCommunicatorModule messagingCommunicatorModule = new MessagingCommunicatorModule(
		        communicator, currentUser.getAccountServer());

		messagingManager = new MessagingManager(messagingCommunicatorModule);

	}

	// ======================================================================
	// {{ Broadcast Methods

	private void broadcastThreadsFetched(List<MessageThread> threads) {

		Intent intent = new Intent();
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setAction(ACTION_ON_THREADS_FETCHED);
		intent.putExtra(EXTRA_FETCHED_THREADS, (ArrayList<MessageThread>) threads);
		sendBroadcast(intent);

	}

	private void broadcastMessagesForThreadFetched(String threadId, List<Message> messages) {

		Intent intent = new Intent();
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setAction(ACTION_ON_MESSAGES_FOR_THREAD_FETCHED);
		intent.putExtra(EXTRA_MESSAGES_THREAD_ID, threadId);
		intent.putExtra(EXTRA_FETCHED_MESSAGES, (ArrayList<Message>) messages);
		sendBroadcast(intent);

	}

	// }}

	// ======================================================================
	// {{ Getters & Setters Methods

	public static boolean isRunning() {
		return isRunning;
	}

	public static void setRunning(boolean isRunning) {
		FetchMessagesService.isRunning = isRunning;
	}

	// }}

}
